/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package theory.vanpelt.bifurcation;

import java.math.BigInteger;

/**
 *
 * Memoizing combinatorics for PDist estimator. Limited to 100 x 100.  
 *
 * @author risto
 */
public class Comb {
	
	/**
	 * Array of precomputed results. Used directly from PDistEstimate.  
	 */
    public static BigInteger comb[] = new BigInteger[100*100];

    /**
     * Compute PDist combinatorial function for a, b.
     * @param a
     * @param b
     * @return
     */
    public static BigInteger comb(int a, int b)
    {
        // a! / b! (a-b)!
        BigInteger af = Fact.fact[a];
        BigInteger bf = Fact.fact[b];
        int ab = a-b;
        if (ab < 0) ab = 0;
        BigInteger abf = Fact.fact[ab];
        BigInteger res = af.divide(bf.multiply(abf));
        return res;
    }

    /**
     * Pre-compute combinatorics for a = 0:100, b = 0:100.
     */
    static {
        for (int a = 0; a < 100; a++) {
            for (int b = 0; b < 100; b++) {
                comb[a+b*100] = comb(a,b);
            }
        }
    }
}
